package cdrprocessing.bolt.stat;

import java.util.Map;

import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

public class CallPerSecBolt extends AbstractTimeWindowBolt {

    private long callCount;

    @Override
    public void prepare(Map stormConf, TopologyContext context) {
        super.prepare(stormConf, context);

        callCount = 0;
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declareStream("CallPerSec", new Fields("count"));
    }

    @Override
    protected void handleNormalTuple(Tuple input, BasicOutputCollector collector) {
        callCount++;
    }

    @Override
    protected void handleTickTuple(Tuple input, BasicOutputCollector collector) {
        collector.emit("CallPerSec", new Values(callCount));
        callCount = 0;
    }

    @Override
    protected int getTimeWindowSize() {
        return 1;
    }

}
